#!/usr/bin/perl

# Process Pend-Moves from registry (win32 Perl executable)
# After install-uninstall - fix/display needed moves.

use Win32API::Registry 0.21 qw( :ALL );
use strict;
sub err{join(' ',@_).': '.t(regLastError())}
my ($type,$from,$to,$key);
RegOpenKeyEx(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager', 0, KEY_ALL_ACCESS, $key);
RegQueryValueEx( $key, "PendingFileRenameOperations", [], $type, $_, [] );
s/(^|\0)[\\\?]+/$1/g;
my @vals=split /\0/,$_;
die "Nothing to pend!" unless @vals;

my @action=();
$\="\n";
while (2==(($from,$to)=(splice @vals,0,2)))
{
my $tos = $to||'DELETE';
print "Pending: $from -> $tos";
$to=~s/^[\!\?\.\\]+?(\w:)/$1/;
my $result=(-e $from)?$to?rename $from,$to:-d $from?rmdir $from:unlink $from:(print("Already done!")||1);

print("Failed to do: $!"),push @action,$from,$to unless $result;
}
RegSetValueEx( $key, "PendingFileRenameOperations", [], $type, join("\0",@action,''), [] );
RegCloseKey($key);
